/*
 * Copyright (C) 2010-2101 Alibaba Group Holding Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.otter.shared.communication.core.impl.dubbo;

import com.alibaba.otter.shared.communication.core.CommunicationEndpoint;
import com.alibaba.otter.shared.communication.core.impl.AbstractCommunicationEndpoint;
import org.apache.dubbo.common.constants.QosConstants;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.ServiceConfig;
import org.apache.dubbo.config.bootstrap.builders.ServiceBuilder;
import org.apache.dubbo.remoting.Constants;

import java.util.Map;

/**
 * 基于dubbo的endpoint实现,仅仅使用了dubb的rpc工具
 *
 * @author jianghang 2011-11-29 上午11:08:29
 * @version 4.0.0
 */
public class DubboCommunicationEndpoint extends AbstractCommunicationEndpoint {


    private ServiceConfig<CommunicationEndpoint> endpoint;

    private int port = 2088;

    private int payload = Constants.DEFAULT_PAYLOAD;

    public DubboCommunicationEndpoint() {

    }

    public DubboCommunicationEndpoint(int port) {
        this.port = port;
    }

    public void initial() {
        // 构造对应的url， String.valueOf() 为避免数字包含千位符
        ServiceBuilder<CommunicationEndpoint> endpointServiceBuilder = ServiceBuilder.newBuilder();
        endpointServiceBuilder.id("endpoint");
        endpointServiceBuilder.path("/endpoint");
        endpointServiceBuilder.interfaceClass(CommunicationEndpoint.class);
        ProtocolConfig protocolConfig = getProtocolConfig();
        endpointServiceBuilder.proxy("javassist");
        endpointServiceBuilder.addProtocol(protocolConfig);
        endpointServiceBuilder.ref(this);
        this.endpoint = endpointServiceBuilder.build();
        endpoint.export();
    }

    private ProtocolConfig getProtocolConfig() {
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setName("dubbo");
        protocolConfig.setHost("127.0.0.1");
        protocolConfig.setPort(port);
        protocolConfig.setPayload(payload);
        protocolConfig.setThreads(50);
        protocolConfig.setIothreads(4);
        protocolConfig.setCodec("dubbo");
        protocolConfig.setServer("netty");
        protocolConfig.setHeartbeat(5000);
        protocolConfig.setSerialization("hessian2");
        protocolConfig.setParameters(Map.of(QosConstants.QOS_ENABLE,"false"));
        return protocolConfig;
    }

    public void destroy() {
        endpoint.unexport();
    }

    // =============== setter / gettter ==================

    public void setPort(int port) {
        this.port = port;
    }

    public void setPayload(int payload) {
        this.payload = payload;
    }

}
